<script setup lang="ts">
import { ref } from 'vue'
import { codeGenerateApi } from '@/apis/code/codeGenerateApi'
import { useEditForm } from '@/components/DataForm/useEditForm'

const emits = defineEmits(['success'])
const dialogFormVisible = ref(false)
const {
  submitEditForm,
  submitLoading,
  editFormData,
  EditForm,
} = useEditForm({ editFields: [
  { name: 'projectName', title: '项目名称', require: true, edit: false },
  { name: 'templateType', title: '代码模版', require: true, dict: 'ENUM_CODE_TEMPLATE_TYPE', editType: 'button' },
] })

function open(projectName: string) {
  editFormData.value = { projectName }
  dialogFormVisible.value = true
}

async function submitForm() {
  await submitEditForm('生成代码', codeGenerateApi.generate)
  dialogFormVisible.value = false
  emits('success')
}

defineExpose({
  open,
})
</script>

<template>
  <el-dialog
    v-model="dialogFormVisible" title="代码生成" width="500"
    destroy-on-close
  >
    <EditForm />
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" :loading="submitLoading" @click="submitForm">
          确认
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>
